﻿/// Author:              LXM
/// Created:             2006-02-06
/// Last Modified:       2009-04-01
using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;
using log4net;

namespace Hutech.Authentication.Library.Web.Framework
{
    public class hhtEncryptionConfiguration
    {
        private static readonly ILog log
        = LogManager.GetLogger(typeof(hhtEncryptionConfiguration));

        private XmlNode rsaNode;

        public void LoadValuesFromConfigurationXml(XmlNode node)
        {
            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.Name == "RSAKeyValue")
                    rsaNode = child;
            }
        }

        public String RsaKey
        {
            get
            {
                if (this.rsaNode != null)
                    return this.rsaNode.OuterXml;

                return String.Empty;
            }
        }

        public static hhtEncryptionConfiguration GetConfig()
        {
            //return (hhtEncryptionConfiguration)ConfigurationManager.GetSection("system.web/hhtEncryption");

            try
            {
                if (
                (HttpRuntime.Cache["hhtEncryptionConfiguration"] != null)
                && (HttpRuntime.Cache["hhtEncryptionConfiguration"] is hhtEncryptionConfiguration)
                )
                {
                    return (hhtEncryptionConfiguration)HttpRuntime.Cache["hhtEncryptionConfiguration"];
                }

                hhtEncryptionConfiguration config
                = new hhtEncryptionConfiguration();

                string pathToConfigFile
                = System.Web.Hosting.HostingEnvironment.MapPath("~/hhtEncryption.config");

                log.Debug("path to crypto key " + pathToConfigFile);

                if (!File.Exists(pathToConfigFile))
                {
                    log.Error("crypto file not found " + pathToConfigFile);
                    return config;
                }

                FileInfo fileInfo = new FileInfo(pathToConfigFile);
                XmlDocument configXml = new XmlDocument();
                configXml.Load(fileInfo.FullName);
                config.LoadValuesFromConfigurationXml(configXml.DocumentElement);

                AggregateCacheDependency aggregateCacheDependency
                = new AggregateCacheDependency();

                aggregateCacheDependency.Add(new CacheDependency(pathToConfigFile));

                System.Web.HttpRuntime.Cache.Insert(
                "hhtEncryptionConfiguration",
                config,
                aggregateCacheDependency,
                DateTime.Now.AddYears(1),
                TimeSpan.Zero,
                System.Web.Caching.CacheItemPriority.Default,
                null);

                return (hhtEncryptionConfiguration)HttpRuntime.Cache["hhtEncryptionConfiguration"];
            }
            catch (HttpException ex)
            {
                log.Error(ex);
            }
            catch (System.Xml.XmlException ex)
            {
                log.Error(ex);
            }
            catch (ArgumentException ex)
            {
                log.Error(ex);
            }
            catch (NullReferenceException ex)
            {
                log.Error(ex);
            }

            return null;
        }
    }
}
